package com.gcu.drivingschool.controller;

import cn.dev33.satoken.secure.BCrypt;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import com.gcu.drivingschool.common.enums.LoginType;
import com.gcu.drivingschool.config.DrivingSchoolLoginMsg;
import com.gcu.drivingschool.domain.dto.LoginDto;
import com.gcu.drivingschool.domain.po.BaseUser;
import com.gcu.drivingschool.domain.po.Coach;
import com.gcu.drivingschool.domain.po.Student;
import com.gcu.drivingschool.domain.vo.Result;
import com.gcu.drivingschool.service.CoachService;
import com.gcu.drivingschool.service.StudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 登录控制器
 * @author zzr
 * @create 2024-04-18-15:42
 */
@Tag(name = "登录控制器", description = "登录控制器")
@RestController
@RequiredArgsConstructor
public class LoginController {
    private final StudentService studentService;
    private final DrivingSchoolLoginMsg drivingSchoolLoginMsg;
    private final CoachService coachService;
    private static final String USER_MSG_KEY="UserMsg";

    /**
     * 登录
     * @param login
     * @return
     */
    @Operation(summary = "登录", description = "登录")
    @PostMapping("/login")
    public Result login(@Validated @RequestBody LoginDto login) {
        String type = login.getType();
        BaseUser user = null;
        if (LoginType.STUDENT.getValue().equals(type)) {//学员登录
            //查询学员表
            user = studentService.lambdaQuery().eq(Student::getTelephone, login.getTelephone()).one();
        } else if (LoginType.COACH.getValue().equals(type)) {//教练登录
            //查询教练表
            user = coachService.lambdaQuery().eq(Coach::getTelephone, login.getTelephone()).one();
        } else if (LoginType.DRIVING_SCHOOL.getValue().equals(type)) {//驾校登录
            //查询配置文件信息
            String telephone = drivingSchoolLoginMsg.getTelephone();
            String password=drivingSchoolLoginMsg.getPassword();
            if (telephone.equals(login.getTelephone())){
                user=new BaseUser(null,telephone,password,null);
            }
        }
        if (user == null) {
            return Result.fail("号码未注册");
        }
        //比较密码
        boolean right = BCrypt.checkpw(login.getPassword(),user.getPassword());
        if (!right){
            return Result.fail("密码错误");
        }
        //保存用户信息到redis
        user.setPassword(null);
        //生成和保存token
        StpUtil.login(type+user.getTelephone());
        SaSession session = StpUtil.getSession();
        session.setType(type);
        session.set(USER_MSG_KEY,user);
        //返回token
        return Result.success("登录成功", StpUtil.getTokenValue());
    }

    /**
     * 退出
     * @return
     */
    @Operation(summary = "退出", description = "退出")
    @DeleteMapping
    public Result loginOut(){
        StpUtil.logout();
        return Result.success("退出成功");
    }

    /**
     * 获取当前登录用户信息
     * @return
     */
    @Operation(summary = "获取当前登录用户信息", description = "获取当前登录用户信息")
    @GetMapping
    public Result getLoginUserMsg(){
        SaSession session = StpUtil.getSession();
        String type = session.getType();
        if (LoginType.STUDENT.getValue().equals(type)) {
            Student student = session.getModel(USER_MSG_KEY, Student.class);
            student.setType(LoginType.STUDENT.getValue());
            return Result.success(student);
        }else if (LoginType.COACH.getValue().equals(type)) {
            Coach coach = session.getModel(USER_MSG_KEY, Coach.class);
            coach.setType(LoginType.COACH.getValue());
            return Result.success(coach);
        }else {
            BaseUser baseUser = session.getModel(USER_MSG_KEY, BaseUser.class);
            baseUser.setType(LoginType.DRIVING_SCHOOL.getValue());
            return Result.success(baseUser);
        }
    }
}
